import requests

import utils

ips = ["192.168.1.29"]


def collect_data(ip, opt):
    url_ip = "http://" + ip + ":9090/api/v1/query?query="
    url = url_ip + opt
    try:
        results = requests.get(url)
        r = results.json()
        return r
    except Exception as e:
        print(e)
        return None


def phys_format(ip, name, data, t):
    value = str(round(float(data['value'][1]), 6))
    return name.rstrip("\n") + ":" + data['metric']['pdu_id'] + "-" + data['metric']['unit_id'] + ":" + ip + ":" + t + ":" + value


def collect_phys(opts, names):
    msgs = []
    for ip in ips:
        ip = ip.rstrip("\n")
        for opt, name in zip(opts, names):
            opt = opt.rstrip("\n")
            r = collect_data(ip, opt)
            for data in r['data']['result']:
                msgs.append(phys_format(ip, name, data, utils.get_ms_time()))
    return msgs
